<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<meta charset="utf-8">
<link rel="stylesheet" href="/css/bootstrap-datetimepicker.min.css" />
<head th:include="include :: header"></head>
<link href="/js/plugins/layui/css/layui.css" rel="stylesheet">
<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">
	<div class="row">
        <div class="col-sm-12">
            <div class="ibox-content">
                <form id="agentForm" class="form-horizontal m-t" >
                    <input id="agentId" name="agentId" th:value="${agent.agentId}" class="form-control" type="hidden">
                    <input type="hidden" th:value="${agent.agentNumber}" name = "agentNumber">
                    <div class="form-group">
                        <label class="col-sm-2 control-label">上级代理商：</label>
                        <div class="col-sm-4" >
                            <span class="form-control" th:text="${parentAgent}"></span>
                        </div>
                        <label class="col-sm-2 control-label">代理商名称：</label>
                        <div class="col-sm-4">
                            <input id="agentName" name="agentName" class="form-control" type="text" th:value="${agent.agentName}" required>
                        </div>
                    </div>
                    <!--<div class="form-group">-->
                        <!--<label class="col-sm-2 control-label">通道分类：</label>-->
                        <!--<div class="col-sm-4">-->
                            <!--<select id="payChannelType" name="payChannelType" class="form-control">-->
                                <!--<option th:each="pct:${payChannelTypes}" th:value="${pct.key}" th:text="${pct.value}" th:selected="${agent.payChannelType==pct.key}"></option>-->
                            <!--</select>-->
                        <!--</div>-->
                    <!--</div>-->
                    <div class="form-group">
                        <label class="col-sm-2 control-label">联系人：</label>
                        <div class="col-sm-4">
                            <input id="contacts" name="contacts"  th:value="${agent.contacts}" class="form-control" maxlength="20" type="text">
                        </div>
                        <label class="col-sm-2 control-label">联系人电话：</label>
                        <div class="col-sm-4">
                            <input id="contactsPhone" name="contactsPhone" th:value="${agent.contactsPhone}" maxlength="20" class="form-control" type="text">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">联系人邮箱：</label>
                        <div class="col-sm-4">
                            <input id="contactsEmail" name="contactsEmail" th:value="${agent.contactsEmail}" maxlength="60" class="form-control" type="email">
                        </div>
                        <label class="col-sm-2 control-label">联系人QQ：</label>
                        <div class="col-sm-4">
                            <input id="contactsQq" name="contactsQq" th:value="${agent.contactsQq}" class="form-control" maxlength="15" type="number">
                        </div>
                    </div>

                    <div class="form-group" th:each="payCoin:${payCoins}">
                        <label class="col-sm-2 control-label" th:text="${payCoin.value + '费率：'}"></label>
                        <div class="col-sm-2">
                            <input th:id ="${'coinRate' + payCoin.key}" class="form-control"  min='0' maxlength="4" type="number" required>
                        </div>
                        <div class="col-sm-4">
                            <label class="control-label">%/笔</label>
                            <label>(父级费率</label>
                            <label th:id ="${'parentAgentRate' + payCoin.key}">0</label>
                            <label>%/笔)</label>
                        </div>
                    </div>
                    <input type="hidden" id="coinRateStr" name = "coinRateStr">
                    <div class="form-group">
                        <div class="col-sm-12 col-sm-offset-3">
                            <button type="submit" class="btn btn-success">提交</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
	<div th:include="include::footer"></div>
	<script th:inline="javascript">
        /*<![CDATA[*/
        payCoins = [[${payCoins}]];
        coinRate = [[${agent.coinRate}]];
        parentCoinRate = [[${parentCoinRate}]];
        /*]]>*/
	</script>
	<script type="text/javascript" src="/js/plugins/layui/layui.js"></script>
	<script type="text/javascript" src="/js/bootstrap-datetimepicker.min.js"></script><!-- 日期框 -->
	<script type="text/javascript" src="/js/bootstrap-datetimepicker.zh-CN.js"></script><!-- 日期框 -->
	<script type="text/javascript">
        $(function(){
            validateRule();
            for(payCoin in payCoins){
                if(coinRate && coinRate[payCoin]){
                    $("#coinRate" + payCoin).val(coinRate[payCoin].rate);
                    if(parentCoinRate){
                        $('#parentAgentRate' + payCoin).text(parentCoinRate[payCoin].rate);
                    }
                }
            }
        })
        $.validator.setDefaults({
            submitHandler : function() {
                save();
            }
        });
        function save() {
            var coinRate = {};
            for(payCoin in payCoins){
                coinRate[payCoin] = {};
                if(parseFloat($("#coinRate" + payCoin).val()) - parseFloat($('#parentAgentRate' + payCoin).text()) < 0){
                    layer.msg("必须大于父级代理商的费率");
                    return ;
                }
                coinRate[payCoin].rate = "" +  $("#coinRate" + payCoin).val();
                coinRate[payCoin].unit =  "1";
            }
            $("#coinRateStr").val(JSON.stringify(coinRate));
            $.ajax({
                cache : true,
                type : "POST",
                url : "/pay/agent/update",
                data : $('#agentForm').serialize(),
                async : false,
                error : function(request) {
                    parent.layer.alert("连接超时!");
                },
                success : function(data) {
                    if (data.code == 0) {
                        parent.layer.msg("操作成功");
                        parent.reLoad();
                        var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                        parent.layer.close(index);
                    } else {
                        parent.layer.alert(data.msg)
                    }
                }
            });

        }
        function validateRule() {
            var icon = "<i class='fa fa-times-circle'></i> ";
            $("#agentForm").validate({
                rules : {
                    agentName : {
                        required : true
                    }
                },
                messages : {
                    agentName : {
                        required : icon + "请输入代理商名称"
                    }
                }
            });
        }



    </script>
</body>
</html>
